package com.hghivln.erpwarehouse.system.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * ApplicationContextUtil：用于更加灵活地获取IOC容器中组件
 * spring容器加载时，会去找classpath下所有实现这些接口的类（ApplicationContextAware），当容器加载完成，会回调类中的以下方法。
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    @Autowired
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            context = applicationContext;
    }

    //public static Object getBean(String name){
    //    return context.getBean(name);
    //}

    public static <T> T getBean(Class<T> cls) {
        return context.getBean(cls);
    }
}
